#include "Channels.h"

/////////////////// RtTimeChannel //////////////////////////////////////
RtTimeChannel::RtTimeChannel(const QString& name) :
	RtDataChannel(name)
{
	dv_ = 0.001;
}

RtTimeChannel::~RtTimeChannel(void)
{
}

void RtTimeChannel::run()
{
	// disabling averaging etc.
	v_ = RtTimeValue::now();
	// time data is always availiable
	dataReady_ = true;

	// do not call RtDataChannel::run
	RtJob::run();
}

/////////////////////// RtTestChannel //////////////////////////////////////
#include "RtEnumHelper.h"
Q_SCRIPT_ENUM(TestType,RtTestChannel)
RtTestChannel::RtTestChannel(const QString& name) :
	RtDataChannel(name), par_(1.), type_(Random), v(0)
{
}
RtTestChannel::~RtTestChannel(void)
{
}
void RtTestChannel::registerTypes(QScriptEngine* e)
{
	qScriptRegisterTestType(e);
	RtDataChannel::registerTypes(e);
}
void RtTestChannel::run()
{
	static int i;

	double t;

	switch (type_)
	{
	case Random:
		v = par_*rand()/RAND_MAX;
		break;
	case Inc:
		v += par_;
		break;
	case Dec:
		v -= par_;
		break;
	case Sin:
		t = RtTimeValue::now();
		v = sin(6.283185307*par_*t);
	}

	push(v);
	RtDataChannel::run();
}
//////////////////// RtFilterChannel /////////////////////////////
RtFilterChannel::RtFilterChannel(const QString& name) : RtDataChannel(name)
{
}
void RtFilterChannel::setInputChannel(RtObject* obj)
{
	RtDataChannel* ch = qobject_cast<RtDataChannel*>(obj);
	if (ch)
	{
		auto_lock L(comm_lock);
		inputChannel_ = ch;
		emit propertiesChanged();
	}
}
RtObject* RtFilterChannel::inputChannel()
{
	if (inputChannel_) return inputChannel_;
	else if (qobject_cast<RtDataChannel*>(parent())) return (RtObject*)parent();
	else return 0;
}
void RtFilterChannel::run()
{
	RtDataChannel* ch = (RtDataChannel*)inputChannel();
	if (ch && ch->dataReady()) push(ch->value());
	RtDataChannel::run();
}
